home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / graphic import.export / completed lab / alphacompositing.c next >
Encoding:
Text File  |  2000-10-06  |  4.1 KB  |  107 lines

  1. // Graphics Importer and Exporter Samples
  2. // This example takes two images and composites them together
  3. // demonstrating the use of different graphic modes
  4. // Originally written by Sam Bushell for QuickTime "Live" '99
  5. // WWDC 2000 Introduction to QuickTime
  6.  
  7. #include "MacShell.h"
  8.  
  9. void AlphaComposite( void )
  10. {
  11.     OSErr err = noErr;
  12.     Handle hOpenTypeList = NewHandle(0);
  13.     long  numTypes = 0;
  14.     FSSpec    theFSSpec;
  15.     GraphicsImportComponent backgroundImporter = 0, foregroundImporter = 0;
  16.     Rect backgroundBounds, windowBounds, foregroundBounds;
  17.     WindowPtr window = NULL;
  18.     MatrixRecord matrix;
  19.     RGBColor whiteColor;
  20.     FixedPoint backgroundCenter;
  21.  
  22.     BuildGraphicsImporterValidFileTypes( hOpenTypeList, &numTypes );
  23.     HLock( hOpenTypeList );
  24.     
  25.     // prompt for a background image.
  26.     err = GetOneFileWithPreview(numTypes, (OSTypePtr)*hOpenTypeList, &theFSSpec, NULL);
  27.     if (err) goto bail;
  28.     err = GetGraphicsImporterForFile( &theFSSpec, &backgroundImporter );
  29.  
  30.     // prompt for a foreground image.
  31.     err = GetOneFileWithPreview(numTypes, (OSTypePtr)*hOpenTypeList, &theFSSpec, NULL);
  32.     if (err) goto bail;
  33.     err = GetGraphicsImporterForFile( &theFSSpec, &foregroundImporter );
  34.     err = GraphicsImportGetNaturalBounds( backgroundImporter, &backgroundBounds );
  35.     
  36.     windowBounds = backgroundBounds;
  37.     OffsetRect( &windowBounds, 10, 45 );
  38.     window = NewCWindow( NULL, &windowBounds, "\pAlpha Composite", true, documentProc, (WindowPtr)-1, true, 0);
  39.     
  40.     // set the graphics port for drawing the first image
  41.     err = GraphicsImportSetGWorld( backgroundImporter, GetWindowPort( window ), NULL );
  42.     
  43.     // draw the background
  44.     err = GraphicsImportDraw( backgroundImporter );
  45.     
  46.     pause();
  47.     
  48.     // center the foreground image over the background image
  49.     // get the native size of the foreground image
  50.     // offset the image to center it
  51.     // set the rectangle in which to draw an image
  52.     err = GraphicsImportGetNaturalBounds( foregroundImporter, &foregroundBounds );
  53.     OffsetRect( &foregroundBounds, 
  54.                 (backgroundBounds.right - foregroundBounds.right) / 2,
  55.                 (backgroundBounds.bottom - foregroundBounds.bottom) / 2 );
  56.     err = GraphicsImportSetBoundsRect( foregroundImporter,
  57.                                        &foregroundBounds );
  58.     
  59.     // draw the foreground image over the background image,
  60.     // using the default graphics mode, ditherCopy.
  61.     err = GraphicsImportSetGWorld( foregroundImporter, GetWindowPort( window ), NULL );
  62.     err = GraphicsImportDraw( foregroundImporter );
  63.  
  64.     pause();
  65.     
  66.     // redraw the background.
  67.     err = GraphicsImportDraw( backgroundImporter );
  68.     
  69.     // draw the foreground using the transparent graphics mode, with white transparent.
  70.     whiteColor.red = 0xffff;
  71.     whiteColor.green = 0xffff;
  72.     whiteColor.blue = 0xffff;
  73.     err = GraphicsImportSetGraphicsMode( foregroundImporter,    // graphics importer instance
  74.                                          transparent,            // graphics transfer mode to use for drawing
  75.                                          &whiteColor );            // color to use for blending and transparent operations
  76.     err = GraphicsImportDraw( foregroundImporter );
  77.  
  78.     pause();
  79.     
  80.     // redraw the background.
  81.     err = GraphicsImportDraw( backgroundImporter );
  82.     
  83.     // draw the foreground using the straight alpha graphics mode.
  84.     err = GraphicsImportSetGraphicsMode( foregroundImporter,        // graphics importer instance
  85.                                          graphicsModeStraightAlpha,    // graphics transfer mode to use for drawing
  86.                                          NULL );
  87.     err = GraphicsImportDraw( foregroundImporter );
  88.  
  89.     pause();
  90.     
  91.     // redraw the background.
  92.     err = GraphicsImportDraw( backgroundImporter );
  93.     
  94.     // draw the foreground *rotated 30 degrees*, using the straight alpha graphics mode.
  95.     err = GraphicsImportGetMatrix( foregroundImporter, &matrix );
  96.     backgroundCenter.x = Long2Fix( backgroundBounds.right - backgroundBounds.left ) / 2;
  97.     backgroundCenter.y = Long2Fix( backgroundBounds.bottom - backgroundBounds.top ) / 2;
  98.     RotateMatrix( &matrix, Long2Fix(30), backgroundCenter.x, backgroundCenter.y );
  99.     err = GraphicsImportSetMatrix( foregroundImporter, &matrix );
  100.     err = GraphicsImportDraw( foregroundImporter );
  101.  
  102. bail:
  103.     if ( backgroundImporter ) CloseComponent( backgroundImporter );
  104.     if ( foregroundImporter ) CloseComponent( foregroundImporter );
  105.     if ( hOpenTypeList)    DisposeHandle( hOpenTypeList );
  106. }
  107.